package com.alex.springbootdemo.security;

import com.alex.springbootdemo.common.ResultCode;
import com.alex.springbootdemo.model.pojo.UmsAdmin;
import com.alex.springbootdemo.utils.AppContext;
import com.alex.springbootdemo.utils.AssertUtil;

import java.util.Optional;

public interface CurrentUser {

    Optional<UmsAdmin> get();

    static UmsAdmin getOrThrow() {
        Optional<UmsAdmin> umsAdmin = AppContext.getInstance(CurrentUser.class).get();
        AssertUtil.isTrue(umsAdmin.isPresent() && umsAdmin.get().getStatus() == 1, ResultCode.USER_NOT_FOUND);
        return umsAdmin.get();
    }

    static UmsAdmin getOrNull() {
        Optional<UmsAdmin> umsAdmin = AppContext.getInstance(CurrentUser.class).get();
        if (umsAdmin.isPresent() && umsAdmin.get().getStatus() == 1) {
            return umsAdmin.get();
        } else {
            return null;
        }
    }
}
